package com.xx.job.service.system.impl;

import com.xx.job.entity.system.SysMenu;
import com.xx.job.entity.system.SysRole;
import com.xx.job.entity.system.SysUser;
import com.xx.job.service.base.BaseService;
import com.xx.job.service.system.SysMenuService;
import com.xx.job.service.system.SysRoleMenuService;
import com.xx.job.service.system.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LoginService extends BaseService {

    @Autowired
    private SysRoleService SysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysMenuService sysMenuService;



    public Map<String,Object> getinfo(HttpServletRequest request)throws Exception{
        Map<String,Object> data = new HashMap<>();
        HttpSession session = request.getSession();
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        // 角色
        SysRole sysRole = SysRoleService.selectByPrimaryKey(sysUser.getRoleId());
        List<String> roleList = new ArrayList<>();
        roleList.add(sysRole.getRoleKey());
        data.put("roles",roleList);

        List<String> permissionsList = new ArrayList<>();
        List<String> buttonsList = new ArrayList<>();
        List<String> types = new ArrayList<>();
        types.add("F");
        List<SysMenu> menuList = sysMenuService.getMenuList(null, sysRole.getRoleId(), types);
        menuList.forEach(x ->{
            permissionsList.add(x.getPermission());
        });
        buttonsList.add("system:sysuser:add");
        data.put("permissions",permissionsList);
        data.put("buttons",buttonsList);
        /*if(sysRole.getRoleKey().equals("admin") || sysRole.getRoleName().equals("系统管理员")){

        }else{
            // Todo 其它权限末完成
        }*/

        // 用户id
        data.put("userId",sysUser.getUserId());
        // 用户名称
        data.put("userName",sysUser.getUsername());
        // 用户动图
        data.put("avatar","http://localhost:8080/img/f778738c-e4f8-4870-b634-56703b4acafe.gif");
       // 部门id
        data.put("deptId", sysUser.getDeptId());
        // 中文名
        data.put("name", sysUser.getNickName());
        return data;
    }
}
